package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class StaffMemberCollection here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StaffMemberCollection
{

       private static ArrayList<StaffMember> staff;
       private static FileDealer file;
       private static StaffMemberCollection instance;
      
    /**
     * Constructor for objects of class StaffMemberCollection
     */
    private StaffMemberCollection()
    {
        file = new FileDealer();
        staff = file.get_data();
        if(staff.isEmpty())
        {
            staff.add(new StaffMember("0", "", "", "", "", "", "Owner", "OwnerPass", 0, true, true, 0, ""));
        }
        
    }
    
    public static void initialize()
    {
        if(instance == null)
        {
            instance = new StaffMemberCollection();
        }
    }
    
    public static StaffMemberCollection get_instance()
    {
        return instance;
    }
    
    
    public static boolean add_staff_member(String name, String phone_nr, String address, String postal_code, String city, String login, String password, int salary, boolean is_manager)
    {
        String id = get_next_account_id(); 
        staff.add(Integer.parseInt(id), new StaffMember(id, name, phone_nr, address, postal_code, city, login, password, salary, is_manager, false, 0, ""));
        return true;
        
    }
    
    public static String get_next_account_id()
    {
    	int max_temp_id = 0;
        int temp_id = 0;
        for (StaffMember member : staff)
        {
            temp_id = Integer.parseInt(member.get_id());
            if(max_temp_id < temp_id)
            {
                if(max_temp_id < (temp_id - 1))
                {
                    if(!(is_such_id(String.valueOf((max_temp_id + 1)))))
                    {
                        break;
                    }
                }
                max_temp_id = temp_id;     
            }
        }
        return String.valueOf((max_temp_id + 1));  
    }
    
    public static String[] give_string_array(String id)
    {
    	return find_account_by_id(id).give_string_array();
    }
    
    public static String print_employee(String id)
    {
        return find_account_by_id(id).printable();
    }
    
    public static boolean delete_account(String id)
    {
        return staff.remove(find_account_by_id(id));
    }
    
    public static String print_all_employees()
    {
        String all_data = "";
        for (StaffMember member : staff)
        {
            all_data += "\n\n" + member.printable();
        }
        return all_data;
    }
    

    /*****************************************************************EDITING FUNCTIONS*****************************************************************/
    public static void edit_name(String id, String name)
    {
        find_account_by_id(id).set_name(name);
    }
    
    public static void edit_phone_nr(String id, String phone_nr)
    {
        find_account_by_id(id).set_phone_nr(phone_nr);
    }
    
    public static void edit_address(String id, String address, String postal_code, String city)
    {
        StaffMember temp = find_account_by_id(id);
        temp.set_address(address);
        temp.set_postal_code(postal_code);
        temp.set_city(city);
    }
      
    public static void edit_salary(String id, int salary)
    {
        find_account_by_id(id).set_salary(salary);
    }
    
    public static void edit_login(String id, String login)
    {
        find_account_by_id(id).set_login(login);
    }
    
    public static void edit_password(String id, String password)
    {
        find_account_by_id(id).set_password(password);
    }
    
    public static void edit_is_manager(String id, boolean is_manager)
    {
        find_account_by_id(id).set_is_manager(is_manager);;
    }        
    
    public static boolean save_data()
    {
        return file.save_changes(staff);
    }
    
    public static StaffMember find_account(String login, String password)
    {
        for (StaffMember member : staff)
        {
            if(member.get_login().equals(login) && member.get_password().equals(password))
            {
                return member;
            }
        }
        return null;
    }
    
    public static boolean is_such_id(String id)
    {
        for(StaffMember member : staff)
        {
            if(member.get_id().equals(id))
            {
                return true;
            }
        }
        return false;
    }
    
    private static StaffMember find_account_by_id(String id)
    {
        for (StaffMember member : staff)
        {
            if(member.get_id().equals(id))
            {
                return member;
            }
        }
        return null;
    }
    
    public static boolean is_login_used(String login)
    {
        for (StaffMember member : staff)
        {
            if (member.get_login().equals(login))
            {
                return true;
            }
        }
        return false;
    }
    
    public static boolean is_manager(String id)
    {
        return find_account_by_id(id).get_is_manager();
    }
    
    public static Object[][] give_all_accounts()
    {
    	int i = 0;
    	Object[][] data = new Object[staff.size()-1][4];
    	for (StaffMember member : staff)
    	{
    		if(!(member.get_is_owner()))
    		{
    			data[i] = member.give_array();
    			i++;
    		}
    	}
    	return data;
    }
    
    public static Object[] give_single_account(String id, boolean is_owner)
    {
    	Object [] data = new Object[4];
    	StaffMember temp = find_account_by_id(id);
    	if(temp != null)
    	{
    		if(!(temp.get_is_owner()))
    		{
    			if(!(!(is_owner) && temp.get_is_manager()))
    			{
    				data = temp.give_array();
    			}
    		}
    	}
    	return data;
    }
    
    public static ArrayList<Object[]> give_accounts_via_name(String name, boolean is_owner)
    {
    	ArrayList<Object[]> data = new ArrayList<>();   	
    	for(StaffMember member : staff)
    	{
    		if(!(member.get_is_owner()))
    		{
	    		if (member.get_name().equals(name) &&(!(!(is_owner) && member.get_is_manager())))
	    		{
	    			data.add(member.give_array());
	    		}
    		}
    	}
    	return data;
    }
   
    
    public static Object[][] give_employee_accounts()
    {
    	int i = 0;
    	Object[][] data = new Object[count_employees()][4];
    	for (StaffMember member : staff)
    	{
    		if(!(member.get_is_manager()))
    		{
    			data[i] = member.give_array();
    			i++;
    		}
    	}
    	return data;
    }
    
    private static int count_employees()
    {
    	int i = 0;
    	for(StaffMember member : staff)
    	{
    		if(!(member.get_is_manager()))
    		{
    			i++;
    		}
    	}
    	return i;
    }
    
    public static String[] get_order_ids(String id)
    {
    	return find_account_by_id(id).get_orders_serviced();
    }
    
    public static Object[] get_account_fields(String id)
    {
    	return find_account_by_id(id).get_fields();
    }
            
            
    


    
    
}
